package com.ruoyi;

import com.ruoyi.common.utils.cmd.CmdAutoStartRedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 启动程序
 *
 * @author ruoyi
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class RuoYiApplication {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static void main(String[] args) {

        SpringApplication.run(RuoYiApplication.class, args);

        System.out.println("(♥◠‿◠)ﾉﾞ  若依启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              \n" +
                "项目访问地址：http://localhost\n" +
                "登录账号：admin\n" +
                "登录账号：admin123\n"
        );
    }

    @PostConstruct
    public void PostConstruct() {
        logger.info("系统启动前-----开始启动redis服务------------------");
        // 单起一个线程，用来启动redis服务
        new Thread(CmdAutoStartRedisUtils::startRedisService).start();
    }

    @PreDestroy
    public void PreDestroy() {
        logger.info("系统停止前-----开始停止redis服务------------------");
        try {
            // 停止redis命令（全路径命令）
            String commandPath = "D:\\gong-zuo\\kai-fa-gong-ju\\redis\\install\\redis-cli shutdown";
            Process process = Runtime.getRuntime().exec(commandPath);
            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 关闭流
            reader.close();
            // 等待命令执行完成
            int exitCode = process.waitFor();
            logger.info("命令执行完成，退出码：" + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
